package com.woniu.elderly.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.ServeAuditDTO;
import com.woniu.common.entity.ServeAudit;
import com.woniu.common.entity.ServePerson;
import com.woniu.elderly.serve.mapper.ServeAuditMapper;
import com.woniu.elderly.serve.service.ServeAuditService;
import com.woniu.elderly.serve.service.ServePersonService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ServeAuditServiceImpl extends ServiceImpl<ServeAuditMapper, ServeAudit> implements ServeAuditService {
    @Autowired
    private ServePersonService servePersonService;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询全部审核
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Page<ServeAudit> findAll(int pageIndex, int pageSize, ServeAuditDTO serveAuditDTO) {
        Wrapper<ServeAudit> wrapper = new LambdaQueryWrapper<>(ServeAudit.class)
                .eq(!StringUtils.isEmpty(serveAuditDTO.getState()), ServeAudit::getState, serveAuditDTO.getState())
                .like(!StringUtils.isEmpty(serveAuditDTO.getAuditperson()), ServeAudit::getAuditperson, serveAuditDTO.getAuditperson())
                .last("ORDER BY FIELD(state, '待审核')DESC, aid ASC")
                ;
        Page<ServeAudit> serveAuditPage = baseMapper.selectPage(new Page<ServeAudit>(pageIndex, pageSize), wrapper);
        return serveAuditPage;
    }

    //根据id查询审核
    @Override
    public ServeAudit findById(int id) {
        return baseMapper.selectById(id);
    }

    //添加审核
    @Override
    public int add(ServeAudit serveAudit) {
        serveAudit.setState("待审核");
        serveAudit.setAuditperson("zc");
        baseMapper.insert(serveAudit);
        return serveAudit.getAid();
    }

    //删除审核
    @Override
    public void remove(int id) {
        baseMapper.deleteById(id);
    }

    //修改审核
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void modify(ServeAudit serveAudit) {
        //获取当前系统时间作为审核时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(date);
        serveAudit.setAudittime(time);

        //修改服务人员状态
        ServePerson servePerson = new ServePerson();
        servePerson.setPid(serveAudit.getPid());
        if(serveAudit.getState().equals("通过")){
            servePerson.setState("离职");
            servePersonService.modify(servePerson);
            servePersonService.remove(servePerson.getPid());
        }else{
            servePerson.setState("空闲");
            servePersonService.modify(servePerson);

            //将服务人员的id存入redis
            SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
            opsForSet.add("ids", servePerson.getPid());
        }
        baseMapper.updateById(serveAudit);
    }
}
